package com.RLFZ.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.Vaccine;
import com.RLFZ.service.VaccineService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.VaccineVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 服务控制器
 *疫苗注射页面
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/vaccine")
public class VaccineController {
    @Autowired
    private VaccineService vaccineService;

    @RequestMapping("/toVaccine")
    public String toHeSuan(){
        return "vaccine/vaccine";
    }
    /**
     * 查询所有角色带有分页，带有条件查询
     */
    @RequestMapping("/loadAllVaccine")
    @ResponseBody
    public DataView loadAllVaccine(VaccineVo vaccineVo){
        QueryWrapper<Vaccine> queryWrapper=new QueryWrapper<>();
        IPage<Vaccine> ipage=new Page<>(vaccineVo.getPage(),vaccineVo.getLimit());
        vaccineService.page(ipage,queryWrapper);
        return new DataView(ipage.getTotal(),ipage.getRecords());
    }

    @RequestMapping("/addVaccine")
    @ResponseBody
    public DataView addOrUpdateVaccine(Vaccine vaccine){
        DataView dataView=new DataView();
        Integer condition = condition(vaccine);
        // 年龄
        if (condition==101){
            dataView.setMsg("修改失败，年龄格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 身份证
        if (condition==102){
            dataView.setMsg("修改失败，身份证格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 手机号
        if (condition==103){
            dataView.setMsg("修改失败，手机号格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 姓名
        if (condition==104){
            dataView.setMsg("修改失败，姓名格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        boolean save = vaccineService.save(vaccine);
        dataView.setMsg("添加疫苗成功");
        dataView.setCode(200);
        return dataView;
    }
    @RequestMapping("/updateVaccine")
    @ResponseBody
    public DataView updateVaccine(Vaccine vaccine){
        DataView dataView=new DataView();
        boolean save = vaccineService.updateById(vaccine);
        dataView.setMsg("修改疫苗成功");
        dataView.setCode(200);
        return dataView;
    }
    @RequestMapping("/deleteVaccine")
    @ResponseBody
    public DataView deleteVaccine(Integer id){
        DataView dataView=new DataView();
        boolean save = vaccineService.removeById(id);
        dataView.setMsg("删除疫苗成功");
        dataView.setCode(200);
        return dataView;
    }
    /**
     * 疫苗注射输入条件判断方法
     * @param vaccine
     * @return
     */
    public Integer condition(Vaccine vaccine){
        Integer age = vaccine.getAge();
        String card = vaccine.getCard();
        String phone = vaccine.getPhone();
        String name = vaccine.getName();
        if (age==null||age<0||age>100){
            return 101;
        }
        if (card==null||card.equals("")||card.length()!=18){
            return 102;
        }
        if (phone==null||phone.equals("")||phone.length()!=11){
            return 103;
        }
        if (name==null||name.equals("")||name.length()<1||name.length()>20){
            return 104;
        }
        return 200;
    }
}